#include"asm_utils.h"
#include"interrupt.h"
#include"stdio.h"
#include"program.h"
#include"thread.h"

//屏幕IO处理器
STDIO stdio;
//中断管理器
InterruptManager interruptManager;
//程序管理器
ProgramManager programManager;

void first_thread(void* arg);
void second_thread(void* arg);
void third_thread(void* arg);

void first_thread(void* arg)
{
	//第一个线程不可以返回
	printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
	if(!programManager.running->pid)
	{
		programManager.executeThread(second_thread,nullptr,"second thread",1);
		programManager.executeThread(second_thread,nullptr,"third thread",1);
	}
	asm_halt();
}
void second_thread(void* arg)
{	
	int i=0;
	while(true)
		printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
}
void third_thread(void* arg)
{
	while(true)
	printf("pid %d, name \"%s\" :Hello ysj!\n",programManager.running->pid,programManager.running->name);
	//while(1){}
}


extern "C" void setup_kernel()
{
	interruptManager.initialize();//中断处理部件
	interruptManager.enableTimeInterrupt();
	interruptManager.setTimeInterrupt((void*)asm_time_interrupt_handler);

	stdio.initialize();//屏幕IO处理部件

	programManager.initialize();//程序管理器
	
	int pid=programManager.executeThread(first_thread,nullptr,"first thread",1);
	if(pid==-1)
	{
		printf("can not execute thread!");
		asm_halt();
	}
	//ListItem* item=programManager.readyPrograms.front();
	//PCB* firstThread=ListItemToPCB(item,tagInGeneralList);
	//firstThread->status=RUNNING;
	//programManager.readyPrograms.pop_front();
	//programManager.running=firstThread;
	//asm_switch_thread(0,firstThread);
	programManager.schedule();
	asm_halt();
}
